const Base = require('./base.js');
const common = require('../../common/common.js');
const moment = require('moment');
//体验分享
module.exports = class extends Base {
    async indexAction() {//查询
        const page = this.get('page') || 1;
        const size = this.get('size') || 10;
        const id = this.get('id') || '';
        const begin = this.get('begin') || moment().subtract(30, "day").format("YYYY-MM-DD");;
        const end = this.get('end') || moment().format("YYYY-MM-DD");;
        const is_verify = this.get('is_verify') || '';
        const is_pass = this.get('is_pass') || '';
        const name = this.get('name') || '';
        const keyword = this.get('keyword') || '';
        const mobile = this.get('mobile') || '';
        const sortField = this.get('sortfield') || 'hiolabs_share.addtime';

        const model = this.model('share');
        let seachdata = {},timesearch={};
        if (name) {
            seachdata['u.name'] = ['like', `%${name}%`];
        }
        if (mobile) {
            seachdata['u.mobile'] = ['like', `%${mobile}%`];
        }
        if (is_verify>=0) {
            seachdata['hiolabs_share.is_verify'] = is_verify;
        }
        if (is_pass>=0) {
            seachdata['hiolabs_share.is_pass'] = is_pass;
        }
        if (keyword) {
            seachdata['hiolabs_share.content'] =  ['like', `%${keyword}%`];
        }

        if (begin || end) {
            if (begin) {
                timesearch['egt'] = Math.floor(moment(begin).valueOf()/1000);
            }
            if (end) {
                timesearch['elt'] = Math.floor(moment(end).add(1, "day").valueOf()/1000);
            }
            seachdata['hiolabs_share.addtime'] = timesearch;
        }
        if (id) {
            seachdata['hiolabs_share.id'] = id;
        }
        let data = await model
        .join({ table: 'user', join: 'left', as: 'u', on: ['userid', 'id'] })
        .where(seachdata).field('hiolabs_share.*,u.nickname,u.name,u.mobile').order([sortField]).page(page, size).countSelect();

        for (const item of data.data) {
            item.nickname = item.nickname?Buffer.from(item.nickname, 'base64').toString():'';
            item.clickcount = await this.model('share_click').where({share_id:item.id}).count();
            item.addtimeText = moment.unix(item.addtime).format('YYYY-MM-DD HH:mm:ss');
        }

        return this.success(data);
    }
    /**
     * 保存
     */
    async saveAction() {
        if (!this.isPost) {
            return false;
        }
        const values = this.post();
        const name = this.post('name');
        let id = this.post('id') || 0;
        const pid = this.post('pid') || 0;
        const model = this.model('share');
        let data = await model.where({ id: ['<>', id], name: name, pid: pid }).find();
        if (!think.isEmpty(data)) {
            return this.fail(400, '名称重复');
        }
        values['update_id'] = think.userId;
        if (id > 0) {
            values['update_time'] = Math.floor((new Date()).getTime() / 1000);
            delete values.id;
            await model.where({ id: id }).update(values);
        } else {
            values['add_time'] = Math.floor((new Date()).getTime() / 1000);
            delete values.id;
            id = await model.add(values);
            values['id'] = id;
        }
        return this.success(values);
    }



async readAction() {
    const model = this.model('share');
    const id = this.get('id');
    const data = await model.where({ id: id }).find();
    return this.success(data);
}
async deleteAction() {

    const model = this.model('share');
    const values = this.post();
    const id = this.post('id');

    let data = await model.where({ id: id }).update({is_delete:1});
    return this.success(data);
}

async passAction(){//取消屏蔽
    const model = this.model('share');
    const id = this.post('id');
    const data = await model.where({ id: id }).update({is_pass:1});
    return this.success(data);

}
async notPassAction(){//屏蔽
    const model = this.model('share');
    const id = this.post('id');
    const data = await model.where({ id: id }).update({is_pass:0});
    return this.success(data);

}


async verifyAction(){
    const model = this.model('share');
    const id = this.post('id');
    const data = await model.where({ id: id }).update({is_verify:1});
    return this.success(data);
}

 /**
     * 分享设置
     */
    async shareSetAction() {//
        let values = this.post();
        const model = this.model('setmodel');
        const odata = await model.where({setmodel:'sharesetting'}).find();
        let now=Math.floor(new Date().getTime()/1000);
        let data={
            setmodel:'sharesetting',
            setting:JSON.stringify(values),
            user_id:think.userId,
            update_time:now
        }
        if(think.isEmpty(odata)){
            await model.add(data);
        }else{
            await model.where({setmodel:'sharesetting'}).update(data);
        }

        return this.success(values);
    }
    async getshareSetAction() {//获取
        const model = this.model('setmodel');
        const odata = await model.where({setmodel:'sharesetting'}).find();
        let r={};
        if(think.isEmpty(odata)){
            r={};
        }else{
            r=JSON.parse(odata.setting);
        }

        return this.success(r);
    }


};